package com.xueyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xueyu.dao.*;
import com.xueyu.domain.HouseBean;
import com.xueyu.domain.Relative.RelativeBean;
import com.xueyu.domain.Stayinfo.StayinfoBean;
import com.xueyu.domain.TermBean;
import com.xueyu.domain.UserVo;
import com.xueyu.domain.bed.Community;
import com.xueyu.domain.bed.bedBean;
import com.xueyu.domain.community.CommunityBean;
import com.xueyu.domain.finance.financeBean;
import com.xueyu.domain.item_Excepts.item_ExceptsBean;
import com.xueyu.domain.outRecord.outRecordBean;
import com.xueyu.domain.personinfo.personinfoBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/open-personinfo")
public class personinfoManageController {

    @Autowired
    bedManageDao bedManageDao;
    @Autowired
    HouseDao houseDao;
    @Autowired
    personinfoDao personinfoDao;
    @Autowired
    CommunityDao communityDao;
    @Autowired
    financeDao financeDao;
    @Autowired
    feeprojDao feeprojDao;
    @Autowired
    StayinfoDao stayinfoDao;
    @Autowired
    outRecordDao outRecordDao;
    @Autowired
    RelativeDao relativeDao;
    @Autowired
    item_ExceptsDao itemExceptsDao;
    @Autowired
    TermDao termDao;

    bedBean bedBean = new bedBean();
    personinfoBean personinfoBean = new personinfoBean();
    HouseBean houseBean = new HouseBean();
    Community communityBean = new Community();
    financeBean financeBean = new financeBean();
    StayinfoBean stayinfoBean = new StayinfoBean();
    outRecordBean outRecordBean = new outRecordBean();
    RelativeBean relativeBean = new RelativeBean();
    item_ExceptsBean itemExceptsBean = new item_ExceptsBean();
    TermBean termBean = new TermBean();

    @RequestMapping(value = "/selectPage")
    @ResponseBody
    public Map selectPage(HttpServletRequest req){
        Map map = new HashMap();
        int current = Integer.parseInt(req.getParameter("Current"));
        int pageSize = Integer.parseInt(req.getParameter("size"));
        String like = req.getParameter("like");
        int flag = Integer.parseInt(req.getParameter("flag"));

        int count = 0;
        if (flag == 1){
            count = personinfoDao.count_state_In_1();
        } else if (flag == 2) {
            count = personinfoDao.count_state_In_2();
        }else {
            count = personinfoDao.count_state_In_3();
        }

        List<item_ExceptsBean> item_exceptsList = itemExceptsDao.select_Max_itemExceptsId_where_personId();
        map.put("item_exceptsList", item_exceptsList);
        return select_data(map, current, pageSize, like, count, flag);
    }

    private Map select_data(Map map, int current, int pageSize, String like, int count, int flag) {
        if(current <= 0) {
            current = 1;
        }
        if (count == 0){
            map.put("count",0);
            map.put("personinfoList",null);
            map.put("code",300);
            return map;
        }
        int totalPages = (count + pageSize - 1) / pageSize;
        if(current > totalPages) {
            current = totalPages;
        }
        int offset = (current - 1) * pageSize;
        if (like == "" && like.isEmpty()) {
            List<personinfoBean> personinfoList;
            if (flag == 1){
                personinfoList = personinfoDao.selectPage_Where_state_In_1(null, offset, pageSize);
            }else if (flag == 2){
                personinfoList = personinfoDao.selectPage_Where_state_In_2(null, offset, pageSize);
            } else{
                personinfoList = personinfoDao.selectPage_Where_state_In_3(null, offset, pageSize);
            }
            if (personinfoList.size() == 0){
                map.put("code", 300);
            }else {
                map.put("code",200);
                map.put("personinfoList", personinfoList);
                map.put("count",count);
            }
        } else {
            List<personinfoBean> personinfoList;
            if (flag == 1){
                personinfoList = personinfoDao.selectPage_Where_state_In_1(like, offset, pageSize);
            }else if (flag == 2){
                personinfoList = personinfoDao.selectPage_Where_state_In_2(like, offset, pageSize);
            } else{
                personinfoList = personinfoDao.selectPage_Where_state_In_3(like, offset, pageSize);
            }
            if (personinfoList.size() == 0){
                map.put("code", 300);
            }else {
                map.put("code",200);
                map.put("personinfoList", personinfoList);
                map.put("count",personinfoList.size());
            }
        }
        return map;
    }

    @RequestMapping(value = "/toAdd")
    @ResponseBody
    public Map toAdd(HttpServletRequest req){
        Map map = new HashMap();
        ServletContext application = req.getServletContext();
        List<UserVo> userList = (List<UserVo>) application.getAttribute("operator");
        map.put("operator", userList);
        selectLeftJOIN(map,"Add");
        return map;
    }
    @RequestMapping(value = "/init-finance")
    @ResponseBody
    public Map init_finance(){
        Map map = new HashMap();

        return map;
    }

    @RequestMapping(value = "/Add-personinfo")
    @ResponseBody
    public Map Add(@RequestBody personinfoBean personinfoBean) throws IOException {
        Map map = new HashMap();

        int personType = Integer.parseInt(personinfoBean.getPersonType());
        int bedId = personinfoBean.getBedId();

        if (personinfoBean.getFaceUrl() == null || personinfoBean.getFaceUrl() == ""){
            personinfoBean.setFaceUrl("null");
        }

        int result = personinfoDao.insert(personinfoBean);
        // 如果新增成功
        if (result != 0){
            System.out.println("personinfo true row " + result);
            // 获取到刚刚新增的personId
            int personId = personinfoDao.selectMaxPersoninfoId();
            // 将数据转移到 financeBean
            financeBean = personinfoBean.getFinanceList().get(0);
            financeBean.setPersonId(personId);
            // 获取付款时间 计算到期时间
            String paidDate = financeBean.getPaidDate();

            // 如果是按日付款
            if (personType == 1){
                Map getDay = calculate_expiration_time(1,paidDate);
                String expireDate = (String) getDay.get("expireDate");
                int daysToNextMonth = (int) getDay.get("daysToNextMonth");
                // set 到financeBean中
                financeBean.setExpireDate(expireDate);
                // 将数据新增到finance表中
                int result2 = financeDao.insert(financeBean);
                if (result2 != 0){
                    map.put("daysToNextMonth",daysToNextMonth);
                    map.put("code",200);
                    System.out.println("finance true row " + result2);
                }else {
                    System.out.println("false");
                }
            }
            // 如果是按月付款
            if (personType == 2){
                Map getDay = calculate_expiration_time(30,paidDate);
                String expireDate = (String) getDay.get("expireDate");
                int daysToNextMonth = (int) getDay.get("daysToNextMonth");
                // set 到financeBean中
                financeBean.setExpireDate(expireDate);
                // 将数据新增到finance表中
                int result2 = financeDao.insert(financeBean);
                if (result2 != 0){
                    map.put("daysToNextMonth",daysToNextMonth);
                    map.put("code",200);
                    System.out.println("finance true row " + result2);
                }else {
                    System.out.println("false");
                }
            }
            // 如果是按年付款
            if (personType == 3){
                Map getDay = calculate_expiration_time(365,paidDate);
                String expireDate = (String) getDay.get("expireDate");
                int daysToNextMonth = (int) getDay.get("daysToNextMonth");
                // set 到financeBean中
                financeBean.setExpireDate(expireDate);
                // 将数据新增到finance表中
                int result2 = financeDao.insert(financeBean);
                if (result2 != 0){
                    map.put("daysToNextMonth",daysToNextMonth);
                    map.put("code",200);
                    System.out.println("finance true row " + result2);
                }else {
                    System.out.println("false");
                }
            }
            int result3 = bedManageDao.update_bed_state_2(bedId);
            if (result3 != 0){
                System.out.println("bed true row " + result3);
            }else {
                System.out.println("Error");
            }
            stayinfoBean.setBedId(bedId);
            stayinfoBean.setPersonId(personId);
            stayinfoBean.setStartTime(personinfoBean.getInTime());
            stayinfoBean.setPrice(financeBean.getStayMoney());
            stayinfoBean.setStayType(1);
            stayinfoBean.setRemark(personinfoBean.getRemark());
            stayinfoBean.setOrderTime(personinfoBean.getInTime());
            int result4 = stayinfoDao.insert(stayinfoBean);
            if (result4 != 0){
                System.out.println("stayInfo true row " + result4);
            }else {
                System.out.println("Error");
            }

//            relativeBean = personinfoBean.getRelativeList().get(0);
//            relativeBean.setPersonId(personId);
//            int result5 = relativeDao.insert(relativeBean);
//            if (result5 != 0){
//                System.out.println("stayInfo true row " + result5);
//            }else {
//                System.out.println("Error");
//            }

            map.put("code",200);
            System.out.println("true");
        }else {
            System.out.println("false");
        }
        return map;
    }

    @RequestMapping(value = "/Upload-personinfo")
    @ResponseBody
    public Map Upload(@RequestParam(value = "fileList", required = false) MultipartFile fileList,
                      @RequestParam(value = "cmd") String cmd, @RequestParam(value = "id") int id,
                      HttpServletRequest req) throws IOException {
        Map map = new HashMap();
        QueryWrapper<personinfoBean> queryWrapper = new QueryWrapper<personinfoBean>();
        boolean flag = false;
        if ("upload".equals(cmd)){
            if (fileList != null && !"".equals(fileList)) {
                String randomFileName = UUID.randomUUID().toString();
                String getFileName = fileList.getOriginalFilename();
                String getFileSuffixName = getFileName.substring(getFileName.lastIndexOf("."));
//            String path = "D:\\exploitTool\\com.XM.exTCode\\IntelliJ IDEA 2023.1-File";
                String path = req.getSession().getServletContext().getRealPath("\\");
//            System.out.println("没用的路径" + nothing);
                Date date = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                String setDate = sdf.format(date);
                String createFileName = "\\image" + "\\upload\\" + setDate + "\\";
                File createFile = new File(path + createFileName);
                if (!createFile.exists()) {
                    createFile.mkdirs();
                }
                String newFileName = randomFileName + getFileSuffixName;
                File targetFile = new File(path + createFileName, newFileName);
                System.out.println("这是什么东西？" + targetFile);
                fileList.transferTo(targetFile);

                //将文件路径set到bean中
                String GetabsolutePath = targetFile.getAbsolutePath();
                String newPath = createFileName + newFileName;
//            String newPath = GetabsolutePath.replace("\\", "/");
                System.out.println("newPath:" + newPath);
                personinfoBean.setFaceUrl(newPath);
                System.out.println("face的绝对路径" + GetabsolutePath);

                // 将图片路径的绝对路径新增到personinfo表中
                int personId = personinfoDao.selectMaxPersoninfoId();
                int result = personinfoDao.insertFaceUrl(newPath,personId);
                if (result != 0){
                    Map get_userList = to_update_face(personId);
                    List<personinfoBean> personinfoList = (List<personinfoBean>) get_userList.get("personinfoList");
                    System.out.println("true");
                    map.put("personinfoList", personinfoList);
                    return map;
                }else {
                    System.out.println("false");
                }
            }
        } else if ("update-face".equals(cmd)) {
            Map get_userList = to_update_face(id);
            List<personinfoBean> personinfoList = (List<personinfoBean>) get_userList.get("personinfoList");


        }

        int personId = personinfoDao.selectMaxPersoninfoId();
        String selectFaceUrlWhereId = personinfoDao.selectFaceUrlWhereId(personId);
        if (selectFaceUrlWhereId != "" || selectFaceUrlWhereId != null){
            flag = true;
            map.put("code",200);
        }
        return map;
    }

    @RequestMapping(value = "/getId")
    @ResponseBody
    public Map getId(int id, String cmd, HttpServletRequest req){
        Map map = new HashMap();
        ServletContext application = req.getServletContext();
        List<UserVo> userList = (List<UserVo>) application.getAttribute("operator");
        QueryWrapper<personinfoBean> queryWrapper = new QueryWrapper<personinfoBean>();
        queryWrapper.eq("personId",id);
        List<personinfoBean> personinfoList = personinfoDao.selectList(queryWrapper);
        selectLeftJOIN(map,null);
        map.put("operator", userList);
        map.put("personifyList",personinfoList);
        return map;
    }

    @RequestMapping(value = "/Update-personinfo")
    @ResponseBody
    public Map Update(@RequestBody personinfoBean personinfoBean){
        Map map = new HashMap();
        QueryWrapper<bedBean> bedBeanQueryWrapper = new QueryWrapper<bedBean>();
        QueryWrapper<personinfoBean> personinfoQueryWrapper = new QueryWrapper<personinfoBean>();
        int personId = personinfoBean.getPersonId();
        int bedId = personinfoBean.getBedId();

        boolean flag = true;

        bedBeanQueryWrapper.eq("bedId", bedId);
        List<bedBean> bedList = bedManageDao.selectList(bedBeanQueryWrapper);
        if (bedList.get(0).getState() != "1"){
            personinfoQueryWrapper.eq("personId", personId);
            List<personinfoBean> personinfoList = personinfoDao.selectList(personinfoQueryWrapper);
            if (personinfoList.get(0).getBedId() == bedId){
                flag = true;
            }else {
                flag = false;
            }
            if (personinfoList.get(0).getPersonType().equals(personinfoBean.getPersonType())){
                map.put("msg", "Unchanged");
                System.out.println("finance not update");
            }else {
                System.out.println("alter finance personType");
                List<financeBean> financeList = personinfoBean.getFinanceList();
                String paidDate = financeList.get(0).getPaidDate();
                int personType = Integer.parseInt(personinfoBean.getPersonType());
                // 如果是按日付款
                if (personType == 1){
                    Map getDay = calculate_expiration_time(1,paidDate);
                    String expireDate = (String) getDay.get("expireDate");
                    int daysToNextMonth = (int) getDay.get("daysToNextMonth");
                    // set 到financeBean中
                    financeBean.setExpireDate(expireDate);
                    // 将数据新增到finance表中
                    int result2 = financeDao.insert(financeBean);
                    if (result2 != 0){
                        map.put("daysToNextMonth",daysToNextMonth);
                        map.put("code",200);
                        System.out.println("finance true row " + result2);
                    }else {
                        System.out.println("false");
                    }
                }
                // 如果是按月付款
                if (personType == 2){
                    Map getDay = calculate_expiration_time(30,paidDate);
                    String expireDate = (String) getDay.get("expireDate");
                    int daysToNextMonth = (int) getDay.get("daysToNextMonth");
                    // set 到financeBean中
                    financeBean.setExpireDate(expireDate);
                    // 将数据新增到finance表中
                    int result2 = financeDao.insert(financeBean);
                    if (result2 != 0){
                        map.put("daysToNextMonth",daysToNextMonth);
                        map.put("code",200);
                        System.out.println("finance true row " + result2);
                    }else {
                        System.out.println("false");
                    }
                }
                // 如果是按年付款
                if (personType == 3){
                    Map getDay = calculate_expiration_time(365,paidDate);
                    String expireDate = (String) getDay.get("expireDate");
                    int daysToNextMonth = (int) getDay.get("daysToNextMonth");
                    // set 到financeBean中
                    financeBean.setExpireDate(expireDate);
                    // 将数据新增到finance表中
                    int result2 = financeDao.insert(financeBean);
                    if (result2 != 0){
                        map.put("daysToNextMonth",daysToNextMonth);
                        map.put("code",200);
                        System.out.println("finance true row " + result2);
                    }else {
                        System.out.println("false");
                    }
                }
            }
            if (personinfoList.get(0).getBedId() == personinfoBean.getBedId()){
                System.out.println("user Unchanged Community");
            }else {
                int result3 = bedManageDao.update_bed_state_2(bedId);
                if (result3 != 0){
                    System.out.println("true");
                }else {
                    System.out.println("false");
                }
            }
        }
        QueryWrapper<personinfoBean> queryWrapper = new QueryWrapper<personinfoBean>();
        queryWrapper.eq("personId",personId);
        int result = personinfoDao.update(personinfoBean,queryWrapper);
        if (result != 0){
            map.put("code",200);
            System.out.println("true");
        }else {
            System.out.println("false");
        }
        return map;
    }

    @RequestMapping(value = "/UpdateState")
    @ResponseBody
    public Map personUpdateState() throws ParseException {
        Map map = new HashMap();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        List<Integer> personIds = new ArrayList<>();
        List<financeBean> financeList = financeDao.selectLeftperson();
        financeList.stream()
                .filter(Objects::nonNull)
                .forEach(element ->{
                    String getExpireDate = element.getMaxExpireDate();
                    Date expireDate = null;
                    try {
                        expireDate = sdf.parse(getExpireDate);
                    } catch (ParseException e) {
                        throw new RuntimeException(e);
                    }
                    if (date.getTime() > expireDate.getTime()) {
                        int personId = element.getPersonId();
                        if (personId != 0) {
                            personIds.add(personId);
                        }
                    }
                });

        if (personIds.size() != 0){
            int result = personinfoDao.updateStateByPersonIds(personIds);
            if (result != 0) {
                map.put("row", result);
                System.out.println("true row: " + result);
            } else {
                System.out.println("Error");
            }
        }

        List<Integer> ids = new ArrayList<>();
        List<personinfoBean> personinfoList = personinfoDao.selectList(null);
        personinfoList.stream()
                .filter(Objects::nonNull)
                .forEach(element ->{
                    if (element.getExpireDate() != null && !element.getExpireDate().equals("")){
                        String leave_time = element.getExpireDate();
                        try {
                            Date leave_transform_time = sdf.parse(leave_time);
                            if (date.getTime() > leave_transform_time.getTime()){
                                int personId = element.getPersonId();
                                if (personId != 0){
                                    ids.add(personId);
                                }
                            }
                        } catch (ParseException e) {
                            throw new RuntimeException(e);
                        }
                    }else {
                        System.out.println("personify none leave_time");
                    }
                });

        if (ids.size() != 0){
            int result2 = personinfoDao.update_personify_state_3(ids);

            if (result2 != 0){
                map.put("leave_user", result2);
                System.out.println("true row " + result2);
            }else {
                System.out.println("Error");
            }
        }

        return map;
    }

    @RequestMapping(value = "/Add-item_excepts")
    @ResponseBody
    public Map Add_excepts(item_ExceptsBean itemExceptsBean){
        Map map = new HashMap();
        int result = itemExceptsDao.insert(itemExceptsBean);
        if (result != 0){
            System.out.println("true row " + result);
        }else {
            System.out.println("Error");
        }
        return map;
    }

    @RequestMapping(value = "/get-item_excepts")
    @ResponseBody
    public Map select_item_excepts(HttpServletRequest req){
        Map map = new HashMap();
        int current = Integer.parseInt(req.getParameter("current"));
        int pageSize = Integer.parseInt(req.getParameter("size"));
        int count = itemExceptsDao.count();
        if(current <= 0) {
            current = 1;
        }
        if (count == 0){
            map.put("count",0);
            map.put("item_exceptsList",null);
            map.put("code",300);
            return map;
        }
        int totalPages = (count + pageSize - 1) / pageSize;
        if(current > totalPages) {
            current = totalPages;
        }
        int offset = (current - 1) * pageSize;

        List<item_ExceptsBean> item_exceptsList = itemExceptsDao.select_Max_itemExceptsId_left_personinfo(offset,pageSize);
        map.put("item_exceptsList",item_exceptsList);
        map.put("count", count);
        return map;
    }


    @RequestMapping(value = "/update_item_excepts_state1")
    @ResponseBody
    public Map update_item_excepts_state1(int itemExceptsId, int personId){
        Map map = new HashMap();
        List<item_ExceptsBean> item_exceptsList = itemExceptsDao.select_Max_item_ExceptsBean_state(personId);
        int state = item_exceptsList.get(0).getExcepts_state();
        if (state != 0){
            map.put("code", 201);
            map.put("msg", "请接收待审核的人物，请不要重复的操作！");
            return map;
        }
        int result = itemExceptsDao.update_item_ExceptsBean_state1(itemExceptsId);
        if (result != 0){
            map.put("code", 200);
            System.out.println("true row " + result);
        }else {
            System.out.println("Error");
        }
        return map;
    }

    @RequestMapping(value = "/update_item_excepts_state2")
    @ResponseBody
    public Map update_item_excepts_state2(int personId, int itemExceptsId, String check_time){
        Map map = new HashMap();
        List<item_ExceptsBean> item_exceptsList = itemExceptsDao.select_Max_item_ExceptsBean_state(personId);
        int state = item_exceptsList.get(0).getExcepts_state();
        if (state != 1){
            map.put("code", 201);
            map.put("msg", "请审核审核中的人物，请不要重复的操作！");
            return map;
        }
        itemExceptsBean.setItemExceptsId(itemExceptsId);
        itemExceptsBean.setCheck_time(check_time);
        int result = itemExceptsDao.update_item_ExceptsBean_state2(itemExceptsBean);
        if (result != 0){
            map.put("code", 200);
            System.out.println("true row " + result);
        }else {
            System.out.println("Error");
        }
        return map;
    }


    @RequestMapping(value = "/item_excepts_deleteBatch")
    @ResponseBody
    public Map item_excepts_deleteBatch(String idArray){
        Map map = new HashMap();
        QueryWrapper<item_ExceptsBean> queryWrapper = new QueryWrapper<item_ExceptsBean>();
        queryWrapper.in("itemExceptsId", idArray.split(","));
        int result = itemExceptsDao.delete(queryWrapper);
        if (result != 0){
            map.put("code", 200);
            System.out.println("true row " + result);
        }else {
            System.out.println("Error");
        }
        return map;
    }

    @RequestMapping(value = "/item_Excepts_cancellation")
    @ResponseBody
    public Map item_Excepts_cancellation(String[] personIds, String[] itemExceptsIds){
        Map map = new HashMap();

        String[] personIde = Arrays.copyOfRange(personIds, 0, personIds.length -1);
        int[] personId = Arrays.stream(personIde).mapToInt(Integer::parseInt).toArray();

        String[] itemExceptsIde = Arrays.copyOfRange(itemExceptsIds, 0, itemExceptsIds.length -1);
        int[] itemExceptsId = Arrays.stream(itemExceptsIde).mapToInt(Integer::parseInt).toArray();
        List<Integer> itemExceptsIdList = Arrays.stream(itemExceptsId).boxed().collect(Collectors.toList());


        List<item_ExceptsBean> item_exceptsList = itemExceptsDao.select_Max_item_ExceptsBean_state_in_id(personId);
        item_exceptsList.stream()
                .filter(Objects::nonNull)
                .forEach(element -> {
                    int state = element.getExcepts_state();
                    if (state == 0) {
                        map.putIfAbsent("msg", "操作失败，数据未接收,请接收数据后在进行操作。");
                        map.putIfAbsent("code", 201);
                    }
                });

        if (map.containsKey("code")) {
            return map;
        }

        int result = itemExceptsDao.update_item_ExceptsBean_state5(itemExceptsIdList);
        if (result != 0){
            map.put("code", 200);
            System.out.println("true row " + result);
        }else {
            System.out.println("Error");
        }
        return map;
    }


    @RequestMapping(value = "/select_community_change")
    @ResponseBody
    public Map select_community_change(HttpServletRequest req){
        Map map = new HashMap();
        QueryWrapper<TermBean> termBeanQueryWrapper = new QueryWrapper<TermBean>();

        int id = Integer.parseInt(req.getParameter("id"));

        if (id == 0){
            id = 1;
        }

        termBeanQueryWrapper.eq("communityId", id);
        List<TermBean> termList = termDao.selectList(termBeanQueryWrapper);
        map.put("termList", termList);
        return map;
    }

    @RequestMapping(value = "/select_term_change")
    @ResponseBody
    public Map select_term_change(int termId, int communityId){
        Map map = new HashMap();
        QueryWrapper<HouseBean> houseBeanQueryWrapper = new QueryWrapper<HouseBean>();
        houseBeanQueryWrapper.eq("termId", termId)
                .eq("communityId", communityId);
        List<HouseBean> houseList = houseDao.selectList(houseBeanQueryWrapper);
        if (houseList.size() != 0){
            map.put("code", 200);
            map.put("houseList", houseList);
        }else {
            map.put("code", 201);
        }
        return map;
    }


    @RequestMapping(value = "/to_update_face")
    @ResponseBody
    public Map to_update_face(int personId){
        Map map = new HashMap();
        QueryWrapper<personinfoBean> queryWrapper = new QueryWrapper<personinfoBean>();
        queryWrapper.eq("personId", personId);
        List<personinfoBean> personinfoList = personinfoDao.selectList(queryWrapper);
        map.put("personinfoList", personinfoList);
        return map;
    }

    @RequestMapping(value = "/update_face")
    @ResponseBody
    public Map update_face(@RequestParam(value = "fileList", required = false) MultipartFile fileList,
                           HttpServletRequest req){
        Map map = new HashMap();
        return map;
    }

    private void selectLeftJOIN(Map map, String cmd) {
        QueryWrapper<bedBean> bedBeanQueryWrapper = new QueryWrapper<>();

        if ("Add".equals(cmd)){
            bedBeanQueryWrapper.eq("state",1);
            List<bedBean> bedBeanList = bedManageDao.selectList(bedBeanQueryWrapper);
            map.put("bed",bedBeanList);
        }else {
            List<bedBean> bedBeanList = bedManageDao.selectList(null);
            map.put("bed",bedBeanList);
        }
        List<HouseBean> houseBeanList = houseDao.houseList_left_term();
        List<Community> communityList = communityDao.selectList(null);
        map.put("house",houseBeanList);
        map.put("Community",communityList);
    }


    public Map calculate_expiration_time(int day, String paidDate){
        Map map = new HashMap();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 获取当前时间o
        LocalTime currentTime = LocalTime.now();
        // 获取这个月的天数
        int currentMonthLength = currentDate.lengthOfMonth();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime paymentTime = LocalDateTime.parse(paidDate, formatter);

        LocalDateTime expiryTime = paymentTime.plusDays(day);
        Date expiryDate = Date.from(expiryTime.atZone(ZoneId.systemDefault()).toInstant());


        // 计算距离下个月还有多少天
        int daysToNextMonth = currentMonthLength - currentDate.getDayOfMonth();
        String expireDate = sdf.format(expiryDate);

        map.put("daysToNextMonth",daysToNextMonth);
        map.put("expireDate", expireDate);

        return map;

    }

    public void UpdateState(String personType,Map map){
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 获取当前时间
        LocalTime currentTime = LocalTime.now();
        if (personType == "1"){
            // 设置时间为凌晨0点整
            LocalTime midnight = LocalTime.of(0, 0, 0);
            // 计算距离凌晨0点整还有多少时间
            Duration timeToMidnight = Duration.between(currentTime, midnight);
            // 判断是否为凌晨0点整
            boolean isMidnight = timeToMidnight.getSeconds() == 0;
            if (isMidnight) {
                int result = personinfoDao.UpdateStatePersonTypeDay();
                if (result != 0) {
                    System.out.println("true row" + result);
                } else {
                    System.out.println("Error");
                }
            } else {
                System.out.println("this time is not midnight");
            }
            map.put("timeToMidnight",timeToMidnight);
            System.out.println("距离下一天还有"+timeToMidnight+"小时");
        }else if (personType == "2"){
            // 判断今天的日期是否为这个月的第一日
            boolean isFirstDayOfMonth = currentDate.getDayOfMonth() == 1;
            if (isFirstDayOfMonth){
                int result = personinfoDao.UpdateStatePersonTypeMonth();
                if (result != 0){
                    System.out.println("true row"+result);
                }else {
                    System.out.println("Error");
                }
            }else {
                System.out.println("this time is not isFirstDayOfMonth");
            }
            // 获取这个月的天数
            int currentMonthLength = currentDate.lengthOfMonth();
            // 计算距离下个月还有多少天
            int daysToNextMonth = currentMonthLength - currentDate.getDayOfMonth();
            map.put("daysToNextMonth",currentMonthLength);
            System.out.println("还剩"+daysToNextMonth+"天");
        }else if (personType == "3"){
            // 判断今年的年份是否为今年的第一月
            boolean isFirstMonthOfYear = currentDate.getMonthValue() == 1;
            if (isFirstMonthOfYear){
                int result = personinfoDao.UpdateStatePersonTypeYear();
                if (result != 0){
                    System.out.println("true row"+result);
                }else {
                    System.out.println("Error");
                }
            }else {
                System.out.println("this time is not isFirstMonthOfYear");
            }
            // 获取今年的天数
            int currentYearLength = currentDate.lengthOfYear();
            // 计算距离下一年还有多少天
            int daysToNextYear = currentYearLength - currentDate.getDayOfYear();
            map.put("daysToNextYear",daysToNextYear);
            System.out.println("还剩"+daysToNextYear+"天");
        }
    }

}
